<?php
	$xtpl_main->assign ( "page_title", "Project" );
	
	//-- Remove Task
	if(isset($_POST['taskId']) && $_POST['taskId'] != ''){
		mysql_query("
						DELETE FROM task WHERE task_id = '".$_POST['taskId']."' AND task_assigned_to = '".$_SESSION['onlineMemberId']."'
					");
		mysql_query("
						DELETE FROM report WHERE task_id = '".$_POST['taskId']."' AND member_id = '".$_SESSION['onlineMemberId']."'
					");
		header("Location: ?act=project&id=".$_POST['projectId']);
	}
	
	//--
	if(isset($_POST['frmAct']) && $_POST['frmAct'] == 'doFeedback'){
		$_POST['feedback_content'] = str_replace("\r", '', $_POST['feedback_content']);
		$_POST['feedback_content'] = preg_replace('/(?<!>)\n/', "<br />\n", $_POST['feedback_content']);
		
		if($_FILES['filedata']['name']){
			$target_path = ROOT_PATH . "uploads/";
			$target_path = $target_path . basename( $_FILES['filedata']['name'] );
			if(move_uploaded_file($_FILES['filedata']['tmp_name'], $target_path)){
				$uploadedFileName = basename( $_FILES['filedata']['name'] );
			}
		}
				
		if($_POST['frmFeedbackId'] == ''){
			mysql_query("
							INSERT INTO feedback 
								(project_id, member_id, feedback_name, feedback_created_date, feedback_content) 
							VALUES 
								('".$_POST['project_id']."', '".$_SESSION['onlineMemberId']."', '".$_POST['feedback_name']."', '".date('Y-m-d')."', '".$_POST['feedback_content']."')
						");
			if(isset($uploadedFileName) && $uploadedFileName != ''){
				mysql_query("UPDATE feedback SET feedback_file = '".$uploadedFileName."' WHERE feedback_id = '".mysql_insert_id()."' LIMIT 1");
			}
		}else{
			mysql_query("
							UPDATE feedback 
							SET 
								project_id = '".$_POST['project_id']."', 
								feedback_name = '".$_POST['feedback_name']."',
								feedback_content = '".$_POST['feedback_content']."',
								feedback_status = '".$_POST['feedback_status']."' 
							WHERE 
								feedback_id = '".$_POST['frmFeedbackId']."' AND member_id = '".$_SESSION['onlineMemberId']."'
						");
			
			if(isset($uploadedFileName) && $uploadedFileName != ''){
				mysql_query("UPDATE feedback SET feedback_file = '".$uploadedFileName."' WHERE feedback_id = '".$_POST['frmFeedbackId']."' LIMIT 1");
			}
			
			/* if($_POST['feedback_status'] == 'closed'){
				mysql_query("
								INSERT INTO task 
									(project_id, task_assigned_to, task_created_date, task_description) 
								SELECT project_id 
							");
			} */
		}
		header("Location: ?act=feedback&id=".$_POST['project_id']);
	}
	
	//-- Select project
	if(isset($_GET['t']) && $_GET['t'] != ''){
		$rsSelectedProjectId = mysql_query("SELECT t.project_id, t.feedback_content, t.feedback_file, t.feedback_name, t.feedback_id FROM feedback t WHERE t.feedback_id = '".$_GET['t']."' AND t.member_id = '".$_SESSION['onlineMemberId']."'");
		if(mysql_num_rows($rsSelectedProjectId) > 0){
			$rowSelectedProjectId = mysql_fetch_array($rsSelectedProjectId);
			$selectedProjectId = $rowSelectedProjectId['project_id'];
			
			$radioClosedChecked = '';
			$radioOpenChecked = '';
			if($rowSelectedProjectId['feedback_status'] != 'closed'){
				$radioOpenChecked = 'checked="checked"';
			}else{
				$radioClosedChecked = 'checked="checked"';
			}
			$feedbackStatus = 	'
									<tr>
										<td>Status:</td>
										<td>
											<input type="radio" value="open" name="feedback_status" '.$radioOpenChecked.' /> Open
											<input type="radio" value="closed" name="feedback_status" '.$radioClosedChecked.' /> Closed
										</td>
									</tr>
								';
			$xtpl->assign("feedbackStatus", $feedbackStatus);
			
			$xtpl->assign("editFeedbackFileName", "<b>Attached file: </b>".$rowSelectedProjectId['feedback_file']);
			$xtpl->assign("editFeedbackName", $rowSelectedProjectId['feedback_name']);
			$xtpl->assign("editFeedbackContent", $rowSelectedProjectId['feedback_content']);
			$xtpl->assign("FeedbackId", $rowSelectedProjectId['feedback_id']);
		}
	}
	
	//-- List select option project
	$rsSelectProject = mysql_query("SELECT project_id, project_name FROM project WHERE 1 ORDER BY project_id DESC");
	while($rowSelectProject = mysql_fetch_array($rsSelectProject)){
		if(isset($selectedProjectId) && $selectedProjectId != '' && $rowSelectProject['project_id'] == $selectedProjectId) $rowSelectProject['project_selected'] = 'selected="selected"';
		$xtpl->assign("listSelectProject", $rowSelectProject);
		$xtpl->parse("center.listSelectProject");
	}
	
//-- Project with task
	if(isset($_GET['id'])){
		$_GET['id'] = intval($_GET['id']);
	
		$rsListProjectWithTask = mysql_query("SELECT project_id, project_name FROM project WHERE project_id = '".$_GET['id']."' LIMIT 1");
		$rowListProjectWithTask = mysql_fetch_array($rsListProjectWithTask);
		
		$rsListTasks = mysql_query("
									SELECT t.*, m.member_displayname FROM feedback t 
									INNER JOIN member m ON m.member_id = t.member_id 
									WHERE t.project_id = '".$rowListProjectWithTask['project_id']."' 
									ORDER BY t.feedback_id DESC
								   ");
		$countTask = mysql_num_rows($rsListTasks);
		if($countTask > 0){
			$iCountTask = $countTask;
			while($rowListTasks = mysql_fetch_array($rsListTasks)){
				if($rowListTasks['member_id'] == $_SESSION['onlineMemberId'] && $rowListTasks['feedback_status'] != 'closed'){
					$temp_url = BASE_URL . 'templates/' . $current_theme . '/images/';
					$rowListTasks['edit_bttn'] = '<a href="?act=feedback&id='.$_GET['id'].'&t='.$rowListTasks['feedback_id'].'"><img alt="Edit this feedback" src="'.$temp_url.'edit-icon.png" style="width: 16px; height: 16px;"></a>&nbsp;';
					$rowListTasks['del_bttn'] = '<a href="#" onclick="doRemoveTask('.$rowListTasks['feedback_id'].');"><img alt="Delete this feedback" src="'.$temp_url.'del-icon.png" style="width: 16px; height: 16px;"></a>&nbsp;';
				}
				
				if($rowListTasks['feedback_file'] != ''){
					$rowListTasks['feedback_attached_file'] = '<a href="'.BASE_URL.'uploads/'.$rowListTasks['feedback_file'].'">download attached file</a>';
				}
				
				$rowListTasks['iCountTask'] = $iCountTask;
				$iCountTask--;
				$xtpl->assign("ListTasks", $rowListTasks);
				$xtpl->parse("center.ListProjectWithTask.ListTasks");
			}
		}
		$xtpl->assign("ListProjectWithTask", $rowListProjectWithTask);
		$xtpl->parse("center.ListProjectWithTask");
		$xtpl->assign("projectId", $_GET['id']);
	}
?>
